﻿// <copyright file="FourByteSensorTeachTelegram.cs" company="Hallvard Korsgaard">
//  Microsoft Public License (Ms-PL). See license details here: http://enoceannet.codeplex.com/license
// </copyright>
// <author>Hallvard Korsgaard</author> 

namespace EnOcean.Radio.Sensor
{
  using System;

  /// <summary>
  /// In order to allow communication between sensors and actuators form different manufacturers a standard teach-in procedure is required.
  /// A receiver needs to know from which type of transmitter a message is coming. To keep the radio telegram as short as possible the
  /// transmitter will inform the receiver during the teach-in procedure about its characteristics.
  /// </summary>
  [Serializable]
  public class FourByteSensorTeachTelegram : FourByteSensorTelegram
  {
    /// <summary>
    /// Gets or sets the sensor fuction.
    /// </summary>
    public FourByteSensorFunction Function
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the device type.
    /// </summary>
    public byte DeviceType
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the manufacturer.
    /// </summary>
    public Manufacturer Manufacturer
    {
      get;
      set;
    }

    /// <summary>
    /// Creates the byte format of the message.
    /// </summary>
    /// <param name="message">A byte array of length 14 to hold the message contents.</param>
    protected internal override void CreateMessage(byte[] message)
    {
      base.CreateMessage(message);
      message[4] = (byte)this.Function;
      message[4] <<= 2;

      message[4] |= (byte)(this.DeviceType >> 5);
      message[5] = (byte)(this.DeviceType << 3);

      message[5] |= (byte)(((short)this.Manufacturer) >> 8);
      message[6] = (byte)this.Manufacturer;
      message[7] = 0x80;
    }
  }
}
